
import java.util.*;


public class XMLSalesFormatter implements SalesFormatter{

    private static XMLSalesFormatter singletonInstance=null;
    private  XMLSalesFormatter(){ }
    public static XMLSalesFormatter getSingletonInstance(){
        if(singletonInstance==null){
            singletonInstance=new XMLSalesFormatter();
        }
        return singletonInstance;
    }

    public String formatSales(Sales sales){
        StringBuilder str=new StringBuilder();
        str=str.append("\n"+"<Sales>"+"\n");
        for(Iterator<Order> a=sales.iterator();a.hasNext();){
            Order order=(Order)a.next();
            str=str.append("  <Order total=\""+order.getTotalCost()+"\">"+"\n");
            OrderItem item;
            for(Iterator<OrderItem> b=order.iterator();b.hasNext();){
                item=(OrderItem)b.next();
                str=str.append("<OrderItem quantity=\"").append(item.getQuantity()+"\"\n");
                str=str.append("price=\"").append(item.getProduct().getPrice()+"\">");
                str=str.append(item.getProduct().getCode()+"</OrderItem>"+"\n"+"</Order>"+"\n");
            }
        }
        return str.toString();
    }
}
